<?php
class Controller_Sprint extends Core_Controller
{
    protected $allowForHttp = array('_getSprints','_getOpenSprints','_getCurrent');

    public function _add()
    {
       $sprintNbr = rand(7, 25);
        $this->view->output = array(
            'success' => true,
            'message' => 'Sprint '.$this->numberToRoman($sprintNbr).' added',
            'data' => array(
                array('sprint' => $sprintNbr, 'name' => 'Sprint '.$this->numberToRoman($sprintNbr), 'startDate' => null, 'endDate' => null)
            )
        );
    }
    public function _getSprints()
    {
        $this->view->output = array(
            'success' => true,
            'message' => '',
            'data' => array(
                array('sprint' => 1, 'name' => 'Sprint '.$this->numberToRoman(1)),
                array('sprint' => 2, 'name' => 'Sprint '.$this->numberToRoman(2)),
                array('sprint' => 3, 'name' => 'Sprint '.$this->numberToRoman(3).' - Current', 'current' => true),
                array('sprint' => 4, 'name' => 'Sprint '.$this->numberToRoman(4)),
                array('sprint' => 5, 'name' => 'Sprint '.$this->numberToRoman(5)),
                array('sprint' => 6, 'name' => 'Sprint '.$this->numberToRoman(6))
            )
        );
    }
    public function _getOpenSprints()
    {
        $this->view->output = array(
            'success' => true,
            'message' => '',
            'data' => array(
                array('sprint' => 0, 'name' => 'Unscheduled'),
                array('sprint' => 3, 'name' => 'Sprint '.$this->numberToRoman(3).' - Current', 'current' => true),
                array('sprint' => 4, 'name' => 'Sprint '.$this->numberToRoman(4)),
                array('sprint' => 5, 'name' => 'Sprint '.$this->numberToRoman(5)),
                array('sprint' => 6, 'name' => 'Sprint '.$this->numberToRoman(6)),
            )
        );
    }
    public function _getCurrent()
    {
        $this->view->output = array(
            'success' => true,
            'message' => '',
            'sprint' => 3
        );
    }

    public function _getAssignees()
    {
        $this->view->output = array(
                'success' => true
                ,'message' => ''
                ,'data' => array(
                        array('userId' => 0, 'username' => 'Unassigned')
                        , array('userId' => 10, 'username' => 'Joona')
                        , array('userId' => 20, 'username' => 'Kalle')
                        , array('userId' => 30, 'username' => 'Eero')
                        , array('userId' => 40, 'username' => 'Terho')
                        , array('userId' => 50, 'username' => 'Ilari')
                        , array('userId' => 60, 'username' => 'Antti')
                        , array('userId' => 70, 'username' => 'Juhani')
                        , array('userId' => 80, 'username' => 'Joel')
                        , array('userId' => 90, 'username' => 'Pekka')
                        , array('userId' => 100, 'username' => 'Hannu')
                )
        );
    }

    

    /**
     * Convert sprint number into roman number
     * @param <type> $num
     * @return <type>
     */
    protected function numberToRoman($num)
    {
        // Make sure that we only use the integer portion of the value
        $n = intval($num);
        $result = '';

        // Declare a lookup array that we will use to traverse the number:
        $lookup = array('M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400,
                'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
                'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);

        foreach ($lookup as $roman => $value)
        {
            // Determine the number of matches
            $matches = intval($n / $value);

            // Store that many characters
            $result .= str_repeat($roman, $matches);

            // Substract that from the number
            $n = $n % $value;
        }

        // The Roman numeral should be built, return it
        return $result;
    }
}
